package jrdcom.com.contentprovidertest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;

/**
 * Created by longcheng on 2017/1/7.
 */
public class myTestContentProvider extends ContentProvider {
    private myDataBaseHelper myDataBaseHelper;

    private final static int STU_DIR= 0;
    private final  static int STU_ITEM = 1;
    private final static String AUTHORITY = "jrdcom.com.contentprovidertest.provider";
    private static UriMatcher uriMatcher;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY,"stu",STU_DIR);
        uriMatcher.addURI(AUTHORITY,"stu/#",STU_ITEM);
    }

    //根据规则,生成Uri


    @Override
    public boolean onCreate() {
        myDataBaseHelper = new myDataBaseHelper(getContext(), "test.db",null, 1);
        return true;
        //return false;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        //return 0;
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        int deleteRows = 0;
        switch (uriMatcher.match(uri)){
            case STU_DIR:
                deleteRows = db.delete("stu",selection, selectionArgs);
                break;
            case STU_ITEM:
                String stu_id = uri.getPathSegments().get(1);
                deleteRows = db.delete("stu","id = ?",new String[]{stu_id});
                break;
        }
        return deleteRows;

    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        Cursor cursor = null;
        switch (uriMatcher.match(uri)){
            case STU_DIR:
                cursor = db.query("stu",projection, selection, selectionArgs, null,null, sortOrder);

                break;
            case STU_ITEM:
                String stu_id = uri.getPathSegments().get(1);
                cursor = db.query("stu",projection, "id = ?", new String[]{stu_id},null,null, sortOrder);
                break;
        }
        //return null;
        return cursor;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        Uri uriReturn = null;
        switch (uriMatcher.match(uri)){
            case STU_DIR:
            case STU_ITEM:
                long stu_id = db.insert("stu",null, values);
                uriReturn = Uri.parse("content://"+AUTHORITY+"/stu/"+stu_id);
                break;
        }
        return uriReturn;
        //return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int updateRows = 0;
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case STU_DIR:
                updateRows = db.update("stu",values,selection,selectionArgs);
                break;
            case STU_ITEM:
                String stu_id = uri.getPathSegments().get(1);
                updateRows = db.update("stu",values,"id = ?", new String[]{stu_id});
                break;

        }
        return updateRows;
    }



    @Nullable
    @Override
    public String getType(Uri uri) {
        String typeString = null;
        switch (uriMatcher.match(uri)){
            case STU_DIR:
                typeString = "vnd.android.cursor.dir/vnd.jrdcom.com.contentprovidertest.provider.stu";
                break;
            case STU_ITEM:
                typeString = "vnd.android.cursor.item/vnd.jrdcom.com.contentprovidertest.provider.stu";
                break;
        }
        //return null;
        return typeString;
    }
}

